package com.weblog.article.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weblog.model.article.Article;
import com.weblog.model.article.ArticleCategory;
import com.weblog.article.service.ArticleService;
import com.weblog.model.article.output.ArticleCusVo;
import com.weblog.model.article.output.ArticleDetailInfo;
import com.weblog.model.article.output.ArticleSeatchVO;
import constants.RedisKey;
import entity.Result;
import entity.ResultMessage;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * article控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private RedisTemplate jsonRedisTemplate;
	/**
	 * 发布文章
	 * @param map
	 */
	@PostMapping()
	public Result publish(@RequestBody Map<String,Object> map) throws ParseException {
		 articleService.publish(map);
		return new Result(true, StatusCode.OK,"发布成功");
	}

	/**
	 * 根据ID查询文章
	 * @param aid ID
	 * @return
	 */
	@GetMapping(value="/{aid}")
	public Result findById(@PathVariable String aid){

		final String key= String.format(RedisKey.ARTICLE_DETAIL,aid);
		Object articleInfo = jsonRedisTemplate.opsForValue().get(key);
		if(articleInfo!=null){
			return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,articleInfo);
		}else {
			ArticleDetailInfo articleDetailInfo = articleService.findById(aid);
			jsonRedisTemplate.opsForValue().set(key,articleDetailInfo,1, TimeUnit.DAYS);
			return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,articleDetailInfo);
		}

	}


	/**
	 * 根据标题和用户id模糊查询文章(自定义分类添加文章时搜索)
	 * @param title 文章标题
	 * @return
	 */
	@GetMapping(value="/searchCA/{title}/{cucId}")
	public Result findByTitleLike(@PathVariable String title, @PathVariable String cucId){

		List<Map<String, String>> maps = articleService.findByTitleLike(title,cucId);
		return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,maps);

	}





	/**
	 * 修改
	 * @param map
	 */
	@PutMapping(value="/{id}")
	public Result update(@RequestBody  Map<String,Object> map, @PathVariable String id ){

		ObjectMapper objectMapper=new ObjectMapper();

		Article article = objectMapper.convertValue(map.get("article"), Article.class);
		ArticleCategory articleCategory = objectMapper.convertValue(map.get("categoryInfo"), ArticleCategory.class);

		article.setAid(id);
		int flag=articleService.update(article,articleCategory);
		if(flag==2){
			return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
		}else if(flag==0){
			//存在重复标签
			return new Result(false, StatusCode.ERROR, ResultMessage.TAGS_REPEATED);
		}
		return new Result(true, StatusCode.OK, ResultMessage.UPDATE_SUCCESS);
	}


	/**
	 * 删除(后台软删除)
	 * @param aid
	 */
	@DeleteMapping(value="/{aid}")
	public Result delete(@PathVariable String aid){
		int flag = articleService.blackById(aid);
		if(flag==2){
			return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
		}
		return new Result(true, StatusCode.OK,"删除成功");
	}

	/**
	 * 审核
	 * @param aid
	 */
	@PutMapping(value="/checkArticle/{aid}")
	public Result checkArticle(@PathVariable String aid){
		int flag=articleService.checkArticle(aid);
		if(flag==2){
			return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
		}
		return new Result(true, StatusCode.OK,"审核成功");
	}

	/**
	 * 点赞
	 * @param aid
	 */
	@PutMapping(value="/thumbup/{aid}")
	public Result thumbup(@PathVariable String aid){
		//判断是否已经点过赞
		String userid= (String) request.getAttribute("id_A");
		String key=String.format(RedisKey.THUMB_UP,userid,aid);
		if(jsonRedisTemplate.opsForValue().get(key)!=null){
			//点过赞
			return new Result(false, StatusCode.REPERROR, ResultMessage.REPEAT_THUMBUP);
		}
		int flag=articleService.thumbup(aid);
		if(flag==0){
			return new Result(false, StatusCode.ERROR,"点赞出了点小错误");
		}
		jsonRedisTemplate.opsForValue().set(key,"1");
		return new Result(true, StatusCode.OK, ResultMessage.THUMBUP_SUCCESS);
	}

	/**
	 * 根据文章id封装搜索文章实体
	 * @return
	 */
	@GetMapping("/client/packageArticle/{aid}")
	public Result packageArticle(@PathVariable("aid")String aid){
		ArticleSeatchVO vo = articleService.packageArticle(aid);
		return new Result(true, StatusCode.OK,"封装成功",vo);

	}

	/**
	 * 根据文章id集合封装自定义分类的文章信息
	 * @param aids 文章id集合
	 * @return 文章信息集合
	 */
	@GetMapping("/client/cuc/articleInfo")
	public Result cucArticle(@RequestParam List<String> aids){
		List<ArticleCusVo> list = articleService.cucArticle(aids);
		return new Result(true, StatusCode.OK,"封装成功",list);

	}


}
